<?php
session_start();
$user=isset($_SESSION["user"])?$_SESSION["user"]:null;
if(!$user){
            echo "未登录，请先登录<a href='login.html'>登录</a>"; 
}
else{

//一页里面显示三行
$pageSize = 3;
$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:1;
$startRow = 0;
$startRow = ($pageIndex-1)*$pageSize;
$userId = $user?$user["Id"]:null;


$sql = "select * from `article`";
$hasWhere = false;
$where = "";
$queryTitle = isset($_GET["Title"])?$_GET["Title"]:null;
if($queryTitle){
    if(!$hasWhere){
        $sql .= " WHERE ";
        $hasWhere = true;
    }
    $where .= "Title like ('%$queryTitle%')";
}

$queryAuthor = isset($_GET["Author"])?$_GET["Author"]:null;
if($queryAuthor){
    if(!$hasWhere){
        $sql .= " WHERE ";
        $hasWhere = true;
    }
    if($where) $where .= " and ";
    $where .= "AuthorName like ('%$queryAuthor%')";
}

$queryMinDate = isset($_GET["minDate"])?$_GET["minDate"]:null;
if($queryMinDate){
    if(!$hasWhere){
        $sql .= " WHERE ";
        $hasWhere = true;
    }
    if($where) $where .= " and ";
    $where .=" CreateTime >= '$queryMinDate'";
}

$queryMaxDate = isset($_GET["maxDate"])?$_GET["maxDate"]:null;
if($queryMaxDate){
    if(!$hasWhere){
        $sql .= " WHERE ";
        $hasWhere = true;
    }
    if($where) $where .= " and ";
    $where .=" CreateTime <= '$queryMaxDate'";
}


$sql = $sql . $where;

$conn = new mysqli("localhost","root","","myblog");
$rows = [];
$sql .= " limit $startRow, $pageSize";
$rs = $conn->query($sql);
while($row = $rs->fetch_assoc()){
    $rows[] = $row;
}
$rs->close(); 

$totalSql = "select count(Id) as total from article";
if($where){
    $totalSql .= " where $where";
}

$rs = $conn->query($totalSql);
$tolal = $rs->fetch_assoc()["total"];

$pageCount = ceil($tolal/$pageSize);//有多少页

function makePageUrl($pageIndex){
    $querystring = "pageIndex=$pageIndex";
    foreach($_GET as $k=>$v){
        if($k==="pageIndex") continue;
        $querystring .= "&$k=$v";
    }
    return $querystring;
}

?>

<!DOCTYPE html>
<html>
        <head>
        <style type="text/css">
        *{
            font-size: 14px;
            padding: 0;
            margin: 0;
            text-align: center;
        }
        table{
            border-collapse: collapse;
            background-color: #ccc;
            border: 1px solid #666;
            width: 100%;
        }
        table td,table th{
            padding: 6px;
            border-collapse: collapse;
            border: 1px solid white;

        }
        table th{
            background-color: #333;
            color: #eee;
        }
        .title{
            width: 300px;
        }
        .aa{
            border-radius: 10px;
            margin: 6px;
        }
        .ss{
            background-color: yellowgreen;
            border: 1px solid white;
            color: white;
            width: 60px;
            height: 60px;
            border-radius: 999px;
            margin: 6px 0 6px 0;
        }
        </style>
            <meta http-equiv="content-type" content="text/httml;charset=utf8"/>
            <div>
                <label>用户名:</label>
                <?=$user["Username"]?>
                <a href="logout.php">退出</a>
            </div>
            <title>文章列表</title>
        </head>
        <body>
            <h3>文章列表</h3>
            <label>网页搜索导航</label>
            <input class="aa" type="text" id="keyword"/>
        <script type="text/javascript">
        document.getElementById("keyword").onblur = function(){
            var key = document.getElementById("keyword").value;
            searchkey(key);
        }
        function searchkey(key,node){
            if(!node)node = document.body;
            for(let i=0,j=node.childNodes.length;i<j;i++){
                let child = node.childNodes[i];
                let val = child.nodeValue;
                if(!val){
                    if(child.childNodes && child.childNodes.length){
                        searchkey(key,child);
                    }else continue;
                }else{
                    var hasKey = val.indexOf(key)>=0;
                    if(hasKey) node.style.border = "1px solid red";
                }
            }
        }
        </script>
            <a href="article_add.php">新建文章</a>
            <form action="" method="GET">
            <div>
                <label>标题</label>
                <input class="aa" type="text" name="Title" id="Title" value="<?=$queryTitle?>"  />
            </div>
            <div>
                <label>作者</label>
                <input class="aa" type="text" name="Author" id="Title" value="<?=$queryAuthor?>"  />
            </div>
            <div>
                <label>时间</label>
                <input class="aa" type="text" name="minDate" value="<?=$queryMinDate?>"/>
                ~<input class="aa" type="text" name="maxDate" value="<?=$queryMaxDate?>"/>
            </div>
                <input class="ss" type="submit" name="submit" value="搜索"/>
            </form>
            <table border="1">
                <thead>
                    <tr>
                        <th class="title">标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($rows as $row){?>    
                <tr>
                    <td>
                    <a href="article_detail.php?Id=<?=$row["Id"]?>"><?=$row["Title"]?></a> 
                    </td>
                    <td>
                    <?=$row["AuthorName"]?>
                    </td>
                    <td>
                    <?=$row["UpdateTime"]?><br/>
                    <?=$row["CreateTime"]?>
                    </td>
                    <td>
                        <a href="article_detail.php?Id=<?=$row["Id"]?>">详细</a> 
                        <?php if($userId===$row["AuthorId"]){?>
                        <a href="article_modify.php?Id=<?=$row["Id"]?>">修改</a> 
                        删除
                        <?php } ?>
                    </td>
                </tr>
                <?php } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="4">
                            共<?=$tolal?>条记录，共<?=$pageCount?>页 当前为<input type='text' value="<?=$pageIndex?>"/>页,
                            <a href="http://localhost/chz/article_list.php?<?=makePageUrl(1)?>">首页</a>
                            <?php for($i = 1;$i<=$pageCount;$i++) {?>
                                <a href="http://localhost/chz/article_list.php?<?=makePageUrl($i)?>"><?=$i?></a>
                            <?php } ?>
                            <a href="http://localhost/chz/article_list.php?<?=makePageUrl($pageCount)?>">尾页</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </body>
<html>
<?php } ?>